//
//  HMUserAccount.swift
//  MyWeibo
//
//  Created by apple on 16/4/19.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HMUserAccount: NSObject ,NSCoding {

    
    /*
    
    "access_token" = "2.00JOxW6GC7DXLE93ab0a5185qFvCBC";
    "expires_in" = 101377;
    "remind_in" = 101377;
    uid = 5874059561;

    */
    var uid :String?
    
    //根据生命周期计算过期时间
    var expires_in :NSTimeInterval = 0 {
        
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    var expiresDate : NSDate?
    
    var remind_in : String?
    var access_token:String?
    var name: String?
    var avatar_large: String?
    
    //kvc
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    //接档归档
    // 解档 --> 如何读取
    required init?(coder decoder: NSCoder) {
        uid = decoder.decodeObjectForKey("uid") as? String
        expires_in = decoder.decodeDoubleForKey("expires_in")
//        expires_in = decoder.decodeIntegerForKey("expires_in")
        access_token = decoder.decodeObjectForKey("access_token") as? String
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
        name = decoder.decodeObjectForKey("name") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        
        //解档过期时间
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
    }
    
    // 归档要调用的方法 --> 如何保存
    func encodeWithCoder(encoder: NSCoder) {
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeDouble(expires_in, forKey: "expires_in")
//        encoder.encodeInteger(expires_in, forKey: "expires_in")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(name, forKey: "name")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        //归档过期时间
        encoder.encodeObject(expiresDate, forKey: "expiresDate")
    }


}
